package studio.raptor.databus.dashboard;

import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@SpringBootApplication
public class DatabusDashboardApplication {

  private static Logger log = LoggerFactory.getLogger(DatabusDashboardApplication.class);

  public static void main(String[] args) {
		SpringApplication.run(DatabusDashboardApplication.class, args);
	}

  @Bean
  public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
  }

  @Bean
  public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:locale/messages");
    messageSource.setCacheSeconds(3600);
    Properties fileEncoding = new Properties();
    try {
      fileEncoding.load(DatabusDashboardApplication.class.getResourceAsStream("/locale/file-encoding.properties"));
    } catch (IOException ioe) {
      log.error("load i18n message file encoding failed.", ioe);
    }
    messageSource.setFileEncodings(fileEncoding);
    return messageSource;
  }
}
